package xj.graph2d.command;

import java.awt.geom.Rectangle2D;

import xj.graph2d.Command;
import xj.graph2d.GraphComponentShape;
import xj.graph2d.Label;
import xj.graph2d.SimpleDrawDocument;

public class LabelAuxAction implements Command.AuxAction {

  public static final int ATTACH_LABEL = 1;

  public static final int DETACH_LABEL = 2;

  public static final int DETACH_ATTACH_LABEL = 3;

  public LabelAuxAction(SimpleDrawDocument doc,
      // DrawCanvas canvas,
      GraphComponentShape fromParent, GraphComponentShape toParent,
      Label label, Label label1, int fromRole, int toRole, int action) {
    this.doc = doc;
    // this.canvas = canvas;
    this.fromParent = fromParent;
    this.toParent = toParent;
    this.label = label;
    this.label1 = label1;
    this.fromRole = fromRole;
    this.toRole = toRole;
    this.action = action;
  }

  public void forward() {
    if (doc != null &&
    // canvas != null &&
	label != null) {
      Rectangle2D r2 = label.getRepaintRect();
      switch (action) {
      case ATTACH_LABEL:
	if (toParent != null) {
	  doc.remove(label);
	  // canvas.repaintRect(r2, true, true);
	  toParent.setLabel(toRole, label);
	  if (label1 != null) {
	    doc.shapeRemoved(label1);
	  }
	  doc.shapeAdded(label);
	}
	break;

      case DETACH_LABEL:
	if (fromParent != null) {
	  fromRole = label.getRole();
	  fromParent.removeLabel(fromRole);
	  // canvas.repaintRect(r2, true, true);
	  doc.shapeRemoved(label);
	  doc.add(label);
	}
	break;

      case DETACH_ATTACH_LABEL:
	if (toParent != null && fromParent != null) {
	  fromRole = label.getRole();
	  fromParent.removeLabel(fromRole);
	  // canvas.repaintRect(r2, true, true);
	  doc.shapeRemoved(label);
	  toParent.setLabel(toRole, label);
	  if (label1 != null) {
	    doc.shapeRemoved(label1);
	  }
	  doc.shapeAdded(label);
	}
	break;
      }
    }
  }

  public void backward() {
    if (doc != null &&
    // canvas != null &&
	label != null) {
      Rectangle2D r2 = label.getRepaintRect();
      switch (action) {
      case ATTACH_LABEL:
	if (toParent != null) {
	  toParent.removeLabel(toRole);
	  // canvas.repaintRect(r2, true, true);
	  doc.shapeRemoved(label);
	  doc.add(label);
	  if (label1 != null) {
	    toParent.setLabel(toRole, label1);
	    doc.shapeAdded(label1);
	  }
	}
	break;

      case DETACH_LABEL:
	if (fromParent != null) {
	  doc.remove(label);
	  // canvas.repaintRect(r2, true, true);
	  fromParent.setLabel(fromRole, label);
	  doc.shapeAdded(label);
	}
	break;

      case DETACH_ATTACH_LABEL:
	if (toParent != null && fromParent != null) {
	  toParent.removeLabel(toRole);
	  // canvas.repaintRect(r2, true, true);
	  doc.shapeRemoved(label);
	  if (label1 != null) {
	    toParent.setLabel(toRole, label1);
	    doc.shapeAdded(label1);
	  }
	  fromParent.setLabel(fromRole, label);
	  doc.shapeAdded(label);
	}
	break;
      }
    }
  }

  public boolean isAlwaysAfter() {
    return true;
  }

  SimpleDrawDocument doc;

  // DrawCanvas canvas;
  GraphComponentShape fromParent;

  GraphComponentShape toParent;

  Label label;

  Label label1;

  int action;

  int fromRole;

  int toRole;

}
